#include "filereaderimpl.h"

#include <QFile>

/*
	Constructor : setup the file name, tree widget icons, and the model.
*/
FileReader::FileReader(const QString &fileName, QTreeWidget *treeWidget, Model *model)
{
    m_fileName = fileName;
    m_model = model;   

    QFile file(m_fileName);
    if (!file.open(QIODevice::ReadOnly))
        return;
    if (!m_doc.setContent(&file))
    {
        file.close();
        return;
    }
    file.close();
}

/*
	Feed the model and the tree widget from the file.
*/
bool FileReader::Read(QTreeWidget *treeWidget)
{
    QString type;
    QDomElement elem;
    QDomElement root = m_doc.documentElement();
    QDomElement node = root.toElement();

	// Loop on the nodes.
    while (!node.isNull())
    {
        ParseElement(node, NULL, treeWidget);
        node = node.nextSiblingElement();
    }
}

/*
	Process nodes.
*/
void FileReader::ParseElement(const QDomElement &element, QTreeWidgetItem *parentItem, QTreeWidget *treeWidget)
{
    QString type;
    QString name;
    QString data;
    QString title;
    QString expanded;
    QTreeWidgetItem *childItem;

    QDomElement child = element.firstChildElement();
    while (!child.isNull())
    {
        name = child.tagName();
        if (name == "element")
        {
            type = child.attribute("type");
            if (type == "CATEGORY")
            {
            	// Get the name
                title = child.firstChildElement("name").text();
                
                expanded = child.attribute("expanded");
                
                // Create the tree item with appropriate parent.
                if (parentItem)
                {
                    childItem = new QTreeWidgetItem(parentItem);
                    childItem->setFlags(parentItem->flags());
                }
                else
                {
                	childItem = new QTreeWidgetItem(treeWidget);                	
               	}                    
                    
                if (expanded == "true")
                	childItem->setExpanded(true);
                else
                	childItem->setExpanded(false);

				// Setup icons and model.
                childItem->setIcon(0, m_model->m_folderIcon);
                childItem->setText(0, title);
                int index = m_model->addItem(title, "CATEGORY", "");
                childItem->setData(0, Qt::UserRole, index);

				// Category can have childs element, process them.
                ParseElement(child, childItem, treeWidget);
            }
            else if (type == "PAGE")
            {
            	// Get name and data.
                title = child.firstChildElement("name").text();
                data = child.firstChildElement("data").text();
                
                // Create the tree item with appropriate parent.
                if (parentItem)
                {
                    childItem = new QTreeWidgetItem(parentItem);
                    childItem->setFlags(parentItem->flags());
                }
                else
                {
                	childItem = new QTreeWidgetItem(treeWidget);
               	}                    
				
				// Setup icons and model.
                childItem->setIcon(0, m_model->m_bookmarkIcon);
                childItem->setText(0, title);
                int index = m_model->addItem(title, "PAGE", data);
                childItem->setData(0, Qt::UserRole, index);
            }
        }
        child = child.nextSiblingElement();
    }
}

QTreeWidgetItem* FileReader::createItem(const QDomElement &element, QTreeWidgetItem *parentItem)
{
    QTreeWidgetItem *item;
    if (parentItem)
    {
        item = new QTreeWidgetItem(parentItem);
    }
    else
    {
        item = new QTreeWidgetItem(parentItem);
    }
    //domElementForItem.insert(item, element);
    return item;
}
